package net.crowdloud.server.DatabaseAccess.problem;

import javax.jdo.PersistenceManager;

import net.crowdloud.client.problem.ProblemDatabaseServerCalls;
import net.crowdloud.server.DatabaseAccess.DatabaseConnection;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * 
 * @Architecture
 */
@SuppressWarnings("serial")
public class ProblemDatabaseImplementation extends RemoteServiceServlet
		implements ProblemDatabaseServerCalls {

	@Override
	public void addProblem(Long userID, net.crowdloud.shared.entities.Problem problem,
			String tags) {

		PersistenceManager persistenceManager = DatabaseConnection.connect();

		SaveProblem.addNew(persistenceManager, userID, problem, tags);

		persistenceManager.close();
	}

	@Override
	public void updateProblem(Long userID,
			net.crowdloud.shared.entities.Problem problem, String tags) {

		if (problem.getProblemID() == null) {

			addProblem(userID, problem, tags);

		} else {

			PersistenceManager persistenceManager = DatabaseConnection
					.connect();

			SaveProblem.update(persistenceManager, problem, tags);

			persistenceManager.close();
		}
	}
}
